//
//  SDLoadingHUD.m
//  SKinDiary
//
//  Created by pcyang on 16/7/13.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDLoadingHUD.h"

#import "SDWindowManager.h"

#define MARGIN 20.0


@interface SDLoadingHUD ()
@property (nonatomic,strong) UIActivityIndicatorView* activityView;
@end

static SDLoadingHUD*  _SDLoadingtView = nil;

@implementation SDLoadingHUD
- (id)initWithView:(UIView *)view {
    if (!view) {
        [NSException raise:@"MBProgressHUDViewIsNillException"
                    format:@"The view used in the MBProgressHUD initializer is nil."];
    }
    self = [self initWithFrame:view.bounds];
    
    if( self )
    {
        self.backgroundColor = [UIColor clearColor];
        self.alpha = 0.0;
        
        self.activityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        [self.activityView startAnimating];
        self.activityView.color = [UIColor blackColor];
        self.activityView.hidesWhenStopped = YES;
        self.activityView.center = CGPointMake(view.bounds.size.width/2, view.bounds.size.height*2/5);
        [self addSubview:self.activityView];
    }
    
    return self;
}

-(void) resetWithSuperView:(UIView*)superView
{
    self.activityView.center = CGPointMake(superView.bounds.size.width/2, superView.bounds.size.height*2/5);
}

//- (void)deviceOrientationDidChange:(NSNotification *)notification {
//	if ([self.superview isKindOfClass:[UIWindow class]]) {
//		[self setTransformForCurrentOrientation:YES];
//	}
//	self.frame = self.superview.bounds;
//	[self setNeedsDisplay];
//}

-(void) show:(BOOL)animated{
    [self setNeedsDisplay];
    [self showUsingAnimation:animated];
}

- (void)showUsingAnimation:(BOOL)animated {
    self.alpha = 0.0;
    
    if (animated) {
        //        self.transform = CGAffineTransformConcat(CGAffineTransformIdentity, CGAffineTransformMakeScale(1.5, 1.5));
        [UIView animateWithDuration:0.2 animations:^{
            self.alpha = 1.0;
            //            self.transform = CGAffineTransformIdentity;
        }];
    }
    else {
        self.alpha = 1.0;
        //        self.transform = CGAffineTransformIdentity;
    }
}

- (void)hide:(BOOL)animated {
    [self hideUsingAnimation:animated];
}
- (void)hideUsingAnimation:(BOOL)animated {
    if (animated) {
        [UIView animateWithDuration:0.2 animations:^{
            //            self.transform = CGAffineTransformConcat(CGAffineTransformIdentity, CGAffineTransformMakeScale(0.5, 0.5));
            self.alpha = 0.02;
        } completion:^(BOOL finished) {
            self.alpha = 0.0;
            [self removeFromSuperview];
        }];
    }
    else {
        self.alpha = 0.0;
        [self removeFromSuperview];
    }
}

+ (void)showLoadingWithSuperView:(UIView *)superView withAnimated:(BOOL)animated center:(CGPoint)center
{
    if ( superView == nil || superView == _SDLoadingtView.superview )
        return;
    
    if (_SDLoadingtView) {
        [_SDLoadingtView resetWithSuperView:superView];
    }
    
    if (_SDLoadingtView == nil) {
        _SDLoadingtView = [[SDLoadingHUD alloc] initWithView:(UIWindow *)superView];
    }
    
    //_SDLoadingtView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [_SDLoadingtView setFrame:superView.bounds];
    [superView addSubview:_SDLoadingtView];
    _SDLoadingtView.center = center;
    [_SDLoadingtView show:animated];
}

+(void) showLoadingWithSuperView:(UIView*) superView withAnimated:(BOOL) animated
{
    
    if(superView == nil)
    {
        //        NSArray* windows =[UIApplication sharedApplication].windows;
        ////        for (UIWindow *w in windows)
        ////        {
        ////            UIWindowLevel lev = w.windowLevel;
        ////            NSString *str ;
        ////        }
        //        if([windows count] >=2)
        //        {
        //            superView = [windows objectAtIndex:1];
        //        }
        //        else
        //        {
        //            superView = [windows objectAtIndex:0];
        //        }
        
        superView = [SDWindowManager defaultManager].currentShowController.view;
        
    }
    
    
    if( superView == _SDLoadingtView.superview )return;
    
    if(_SDLoadingtView)
    {
        [_SDLoadingtView  resetWithSuperView:superView];
    }
    
    if (_SDLoadingtView == nil) {
        _SDLoadingtView = [[SDLoadingHUD alloc] initWithView:(UIWindow*)superView];
    }
    //_SDLoadingtView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [_SDLoadingtView setFrame:superView.bounds];
    [superView addSubview:_SDLoadingtView];
    [_SDLoadingtView show:animated];
}

+(void) hideLoadingWithAnimted:(BOOL) animated
{
    if (_SDLoadingtView == nil)
    {
        return;
    }
    
    [_SDLoadingtView hide:animated];
}

+(void) hideLoadingFromSuperView:(UIView*) superView withAnimted:(BOOL) animated
{
    if (_SDLoadingtView == nil || _SDLoadingtView.superview != superView)
    {
        return;
    }
    
    [_SDLoadingtView hide:animated];
}
@end

